package com.example.testrecorder.utils;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import androidx.annotation.NonNull;

import com.example.testrecorder.R;


public class MyDialog extends Dialog {
    private Button positiveBtn,negativeBtn;
    private TextView title,content;
    private  View middleLine;
    public MyDialog(@NonNull Context context) {
        super(context, R.style.custom_dialog);
        LayoutInflater inflater = LayoutInflater.from(context);
        View v = inflater.inflate(R.layout.my_dialog, null);
        title=v.findViewById(R.id.title);
        content=v.findViewById(R.id.content);
        positiveBtn=v.findViewById(R.id.positiveBtn);
        negativeBtn=v.findViewById(R.id.negativeBtn);
        middleLine=v.findViewById(R.id.middleLine);
        GradientDrawable gradientDrawable=new GradientDrawable();
        gradientDrawable.setCornerRadius(50);
        gradientDrawable.setColor(Color.parseColor("#FFFFFF"));
        v.setBackground(gradientDrawable);
        setCancelable(false);
        setContentView(v);
    }

    /**
     * 两个参数的构造器
     * @param context 上下文
     * @param bgColor 对话框背景颜色
     */
    public MyDialog(Context context,int bgColor){
        super(context, R.style.custom_dialog);
        LayoutInflater inflater = LayoutInflater.from(context);
        View v = inflater.inflate(R.layout.my_dialog, null);
        title=v.findViewById(R.id.title);
        content=v.findViewById(R.id.content);
        positiveBtn=v.findViewById(R.id.positiveBtn);
        negativeBtn=v.findViewById(R.id.negativeBtn);
        middleLine=v.findViewById(R.id.middleLine);
        GradientDrawable gradientDrawable=new GradientDrawable();
        gradientDrawable.setCornerRadius(50);
        gradientDrawable.setColor(bgColor);
        v.setBackground(gradientDrawable);
        setCancelable(false);
        setContentView(v);
    }

    /**
     * 设置对话框标题
     * @param title
     */
    public void setTitle(String title){
        if(title!=null){
           this.title.setText(title);
        }
    }

    /**
     * 设置正文内容
     * @param content
     */
    public void setContent(String content){
        this.content.setText(content);
    }

    /**
     * 设置积极按钮的文字
     * @param positiveText
     */
    public void setPositiveText(String positiveText){
        positiveBtn.setText(positiveText);
    }

    /**
     * 设置标题背景颜色
     * @param color
     */
    public void setTitleBackground(String color){
        if(title!=null){
            GradientDrawable gradientDrawable=new GradientDrawable();
            gradientDrawable.setCornerRadii(new float[]{50,50,50,50,0,0,0,0});
            gradientDrawable.setColor(Color.parseColor(color));
            title.setBackground(gradientDrawable);
        }
    }

    /**
     * 设置标题文字颜色
     * @param color
     */
    public void setTitleTextColor(String color){
        if(title!=null){
            title.setTextColor(Color.parseColor(color));
        }
    }

    /**
     * 设置正文文字颜色
     * @param color
     */
    public void setContentTextColor(String color){
        if(content!=null){
            content.setTextColor(Color.parseColor(color));
        }
    }

    /**
     * 设置消极按钮文字颜色
     * @param color
     */
    public void setNegativeBtnTextColor(String color){
        if(negativeBtn!=null){
            negativeBtn.setTextColor(Color.parseColor(color));
        }
    }

    /**
     * 设置积极按钮文字颜色
     * @param color
     */
    public void setPositiveBtnTextColor(String color){
        if(positiveBtn!=null){
            positiveBtn.setTextColor(Color.parseColor(color));
        }
    }

    /**
     * 设置标题是否可见
     * @param isVisible
     */
    public void setTitleVisibility(boolean isVisible){
        setViewVisibility(title,isVisible);
    }
    /**
     * 设置消极按钮是否可见
     * @param isVisible
     */
    public void setNegativeBtnVisibility(boolean isVisible){
        setViewVisibility(negativeBtn,isVisible);
    }
    /**
     * 设置积极按钮是否可见
     * @param isVisible
     */
    public void setPositiveBtnVisibility(boolean isVisible){
        setViewVisibility(positiveBtn,isVisible);
    }
    /**
     * 设置按钮中间的线是否可见
     * @param isVisible
     */
    public void setMiddleLineVisibility(boolean isVisible){
        setViewVisibility(middleLine,isVisible);
    }

    /**
     * 设置视图是否可见
     * @param isVisible
     */
    private void setViewVisibility(View view,boolean isVisible){
        if(view!=null){
            if(isVisible){
                view.setVisibility(View.VISIBLE);
            }else {
                view.setVisibility(View.GONE);
            }
        }
    }
    public void setOnNegativeListener(View.OnClickListener listener){
        negativeBtn.setOnClickListener(listener);
    }
    public void setOnPositiveListener(View.OnClickListener listener){
        positiveBtn.setOnClickListener(listener);
    }

    public static void showInfo(Activity activity,String info){
        String inf=info;
        if(info.length()>=200){
            inf=info.substring(0,200);
        }
        MyDialog myDialog=new MyDialog(activity,Color.parseColor("#F4F8FB"));
        myDialog.setTitleVisibility(false);
        myDialog.setNegativeBtnVisibility(false);
        myDialog.setMiddleLineVisibility(false);
        myDialog.setContent(inf);
        myDialog.show();
        myDialog.setOnPositiveListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                myDialog.dismiss();
            }
        });
    }

}
